#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：4.pymysql-表创建.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-11 16:42
'''
import pymysql


def create_table():
    # 1.创建连接对象与游标
    with pymysql.connect(host="localhost", user="root", password="123456", port=3306, db="tuling_python") as db:
        with db.cursor() as cursor:
            # 2.创建表之前需要检查表是否存在
            cursor.execute("drop table if exists employee")

            sql = """
                 create table employee (            
                    first_name varchar(20) not null,            
                    last_name varchar(20),            
                    age int,            
                    sex varchar(1),            
                    income float,            
                    create_time datetime
                );
            """

            try:
                # 创建表不需要提交事务
                cursor.execute(sql)
                print("表创建成功")
            except Exception as e:
                print("表创建失败：", e)

if __name__ == '__main__':
    create_table()